/**
 * 融媒数据模拟数据生成服务
 * 提供用于测试和演示的模拟数据
 */

import { MediaPlatform, PlatformMetrics, PlatformTimeSeriesData, DataOverview } from '../../types/mediaData'
import { HotTopic, KeywordData, ContentSuggestion } from '../../types/hotspot'

/**
 * 平台元数据配置
 */
export const PLATFORM_META: Record<MediaPlatform, { name: string; color: string }> = {
  [MediaPlatform.WEIBO]: { name: '微博', color: '#E6162D' },
  [MediaPlatform.WECHAT]: { name: '公众号', color: '#07C160' },
  [MediaPlatform.BILIBILI]: { name: 'B站', color: '#00A1D6' },
  [MediaPlatform.DOUYIN]: { name: '抖音', color: '#000000' },
  [MediaPlatform.XIAOHONGSHU]: { name: '小红书', color: '#FF2442' },
  [MediaPlatform.TOUTIAO]: { name: '头条', color: '#D32F2F' },
  [MediaPlatform.ZHIHU]: { name: '知乎', color: '#0084FF' },
  [MediaPlatform.KUAISHOU]: { name: '快手', color: '#FF6600' },
}

/**
 * 生成模拟平台数据
 */
export function generateMockPlatformMetrics(): PlatformMetrics[] {
  const platforms = Object.values(MediaPlatform)

  return platforms.map(platform => {
    const views = Math.floor(Math.random() * 1000000 + 100000)
    const likes = Math.floor(Math.random() * 50000 + 5000)
    const comments = Math.floor(Math.random() * 10000 + 1000)
    const shares = Math.floor(Math.random() * 5000 + 500)

    return {
      platform,
      platformName: PLATFORM_META[platform].name,
      views,
      likes,
      comments,
      shares,
      followers: Math.floor(Math.random() * 100000 + 10000),
      engagementRate: (likes + comments + shares) / views,
      avgViews: Math.floor(views / (Math.random() * 50 + 10)),
      trend: ['up', 'down', 'stable'][Math.floor(Math.random() * 3)] as 'up' | 'down' | 'stable',
      changeRate: (Math.random() - 0.5) * 50, // -25% ~ +25%
    }
  })
}

/**
 * 生成时间序列数据
 */
export function generateTimeSeriesData(days: number = 30): PlatformTimeSeriesData[] {
  const platforms = [
    MediaPlatform.WEIBO,
    MediaPlatform.DOUYIN,
    MediaPlatform.BILIBILI,
    MediaPlatform.XIAOHONGSHU
  ]

  const metrics = ['播放量', '点赞数', '评论数']

  return platforms.flatMap(platform =>
    metrics.map(metric => ({
      platform,
      platformName: PLATFORM_META[platform].name,
      metric,
      data: Array.from({ length: days }, (_, i) => {
        const baseValue = metric === '播放量' ? 10000 : metric === '点赞数' ? 500 : 100
        const trend = 1 + (i / days) * 0.5 // 增长趋势
        const randomFactor = 0.7 + Math.random() * 0.6 // 随机波动

        return {
          date: new Date(Date.now() - (days - i - 1) * 86400000).toISOString().split('T')[0],
          value: Math.floor(baseValue * trend * randomFactor)
        }
      })
    }))
  )
}

/**
 * 生成数据概览
 */
export function generateDataOverview(platformData: PlatformMetrics[]): DataOverview {
  const totalViews = platformData.reduce((sum, p) => sum + p.views, 0)
  const totalLikes = platformData.reduce((sum, p) => sum + p.likes, 0)
  const totalComments = platformData.reduce((sum, p) => sum + p.comments, 0)
  const totalShares = platformData.reduce((sum, p) => sum + p.shares, 0)
  const totalFollowers = platformData.reduce((sum, p) => sum + p.followers, 0)
  const avgEngagementRate = platformData.reduce((sum, p) => sum + p.engagementRate, 0) / platformData.length
  const topPlatform = [...platformData].sort((a, b) => b.views - a.views)[0].platform

  return {
    totalViews,
    totalLikes,
    totalComments,
    totalShares,
    totalFollowers,
    avgEngagementRate,
    topPlatform,
  }
}

/**
 * 生成热点话题
 */
export function generateMockHotTopics(): HotTopic[] {
  const topics = [
    { title: '春节档电影票房破纪录', platform: MediaPlatform.WEIBO, category: '娱乐' },
    { title: '龙年新春祝福短视频创作', platform: MediaPlatform.DOUYIN, category: '节日' },
    { title: '2025年度职业规划指南', platform: MediaPlatform.ZHIHU, category: '职场' },
    { title: '年夜饭制作教程', platform: MediaPlatform.XIAOHONGSHU, category: '美食' },
    { title: 'AI技术发展趋势', platform: MediaPlatform.TOUTIAO, category: '科技' },
    { title: '冬季旅游攻略', platform: MediaPlatform.XIAOHONGSHU, category: '旅游' },
    { title: '新能源汽车推荐', platform: MediaPlatform.BILIBILI, category: '汽车' },
    { title: '健身减肥计划', platform: MediaPlatform.DOUYIN, category: '健康' },
    { title: '春运出行指南', platform: MediaPlatform.WEIBO, category: '生活' },
    { title: '年货采购清单', platform: MediaPlatform.XIAOHONGSHU, category: '购物' },
  ]

  return topics.map((topic, index) => ({
    id: `hot-${index}`,
    title: topic.title,
    platform: topic.platform,
    heat: Math.floor(Math.random() * 40 + 60), // 60-100
    trend: ['up', 'stable', 'down'][Math.floor(Math.random() * 3)] as 'up' | 'down' | 'stable',
    changeRate: (Math.random() - 0.3) * 100,
    keywords: topic.title.split(/[，。、]/),
    category: topic.category,
    createdAt: new Date(Date.now() - Math.random() * 86400000 * 7),
    updatedAt: new Date(),
  }))
}

/**
 * 生成关键词数据
 */
export function generateKeywordData(): KeywordData[] {
  const keywords = [
    '春节', 'AI', '旅游', '美食', '健康', '科技', '娱乐', '职场',
    '新年', '电影', '短视频', '教程', '攻略', '推荐', '计划', '趋势',
    '创作', '内容', '热点', '话题', '数据', '分析', '优化', '增长',
    '营销', '运营', '设计', '开发', '产品', '用户', '体验', '服务'
  ]

  return keywords.map(keyword => ({
    name: keyword,
    value: Math.floor(Math.random() * 100 + 20)
  }))
}

/**
 * 生成创作建议
 */
export function generateContentSuggestions(): ContentSuggestion[] {
  return [
    {
      id: 'suggestion-1',
      title: '春节档电影深度解析',
      topic: '春节档电影票房破纪录',
      platform: [MediaPlatform.WEIBO, MediaPlatform.BILIBILI],
      reason: '结合当前热门电影，制作深度影评或幕后揭秘内容，预计可获得高互动',
      keywords: ['春节', '电影', '票房', '影评'],
      heat: 95,
    },
    {
      id: 'suggestion-2',
      title: '龙年祝福短视频创意合集',
      topic: '龙年新春祝福短视频创作',
      platform: [MediaPlatform.DOUYIN, MediaPlatform.KUAISHOU],
      reason: '节日热点，适合制作创意祝福视频获取高流量，建议添加特效和音乐',
      keywords: ['龙年', '新春', '祝福', '短视频'],
      heat: 88,
    },
    {
      id: 'suggestion-3',
      title: '2025职场技能提升指南',
      topic: '2025年度职业规划指南',
      platform: [MediaPlatform.ZHIHU, MediaPlatform.TOUTIAO],
      reason: '年初职场规划需求旺盛，可制作系列干货内容，建立专业形象',
      keywords: ['职场', '技能', '规划', '指南'],
      heat: 76,
    },
    {
      id: 'suggestion-4',
      title: '年夜饭硬菜教程系列',
      topic: '年夜饭制作教程',
      platform: [MediaPlatform.XIAOHONGSHU, MediaPlatform.DOUYIN],
      reason: '春节前夕美食内容需求大，图文和视频结合效果更佳',
      keywords: ['年夜饭', '美食', '教程', '硬菜'],
      heat: 82,
    },
  ]
}

/**
 * 生成热点趋势数据
 */
export function generateHotspotTrends(topics: HotTopic[]): Array<{ topic: string; data: Array<{ date: string; value: number }> }> {
  return topics.slice(0, 5).map(topic => ({
    topic: topic.title,
    data: Array.from({ length: 7 }, (_, i) => ({
      date: new Date(Date.now() - (6 - i) * 86400000).toISOString().split('T')[0],
      value: Math.floor(topic.heat * (0.6 + Math.random() * 0.4))
    }))
  }))
}
